﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UFIDA.U9.SCM.SM.SOUIModel;
using UFSoft.UBF.UI.WebControlAdapter;
using UFSoft.UBF.UI.ControlModel;
using UFSoft.UBF.UI.MD.Runtime;
using UFSoft.UBF.UI.Engine.Builder;
using System.Data;
using UFSoft.UBF.Util.DataAccess;
using UFSoft.UBF.Sys.Database;
using UFSoft.UBF.UI.ActionProcess;
using UFSoft.UBF.UI.WebControls.Association.Adapter;
using UFSoft.UBF.UI.WebControls.Association;
using UFSoft.UBF.UI.WebControls.ClientCallBack;
using System.Collections;
using System.Collections.Specialized;
using UFSoft.UBF.UI.Controls;
using UFSoft.UBF.UI.WebControls;
using UFIDA.U9.UI.PDHelper;
using UFSoft.UBF.Business;
using UFSoft.UBF.View.Query;
using UFSoft.UBF.UI.IView;
using UFIDA.U9.Cust.Cli.PlugUI;

namespace UFIDA.U9.Cust.U9Demo.PlugUI
{
    public class RcvUIFormWebPart : UFSoft.UBF.UI.Custom.ExtendedPartBase
    {
        private UFIDA.U9.SCM.PM.ReceivementUIModel.ReceivementMainUIFormWebPart part;
        IUFDataGrid DataGrid4;
        IUFButton btnPRToPM2;
        private IUFButton BtnCusReload = new UFWebButtonAdapter();

        //配置文件WebPartExtend_XX.config文件拷贝到portal下，dll文件放置到portal/UILib下
        public override void AfterInit(UFSoft.UBF.UI.IView.IPart Part, EventArgs args)
        {
            //首先调用原来的事件
            base.AfterInit(Part, args);
            part = Part as UFIDA.U9.SCM.PM.ReceivementUIModel.ReceivementMainUIFormWebPart;

            if (part == null)
                return;

            DataGrid4 = (IUFDataGrid)part.GetUFControlByName(part.TopLevelContainer, "DataGrid4");

            #region 1.Toobar上添加按钮：页面扩展-加按钮
            // （1）、实例化按钮
            IUFButton btnPRToPM = new UFWebButtonAdapter();
            
            //（2）、加入功能栏Card中
            IUFToolbar toolbar = (IUFToolbar)part.GetUFControlByName(part.TopLevelContainer, "Toolbar1");

            if (toolbar != null)
            {
                string guid = "047FC9F5-46C0-449A-83C2-2822BCF24012";// 在数据库生成下GUID，或者修改下这里的值。SELECT NEWID()
                btnPRToPM = UIControlBuilder.BuilderToolbarButton(toolbar, "True", "btnPRToPM", "True", "True", 70, 28, "7", "", true, false,
                    guid, guid, guid);
                UIControlBuilder.SetButtonAccessKey(btnPRToPM);
                btnPRToPM.Text = "拉单销售订单";
                btnPRToPM.ID = "BtnQuerySaleOrder";
                btnPRToPM.AutoPostBack = true;
                btnPRToPM.UIModel = part.Model.ElementID;
                ((UFWebToolbarAdapter)toolbar).Items.Add(btnPRToPM as System.Web.UI.WebControls.WebControl);
                btnPRToPM.Click += new EventHandler(btnPRToPM_Click);


            }
            #endregion

            //2.Card里面增加按钮
            //设置按钮在容器中的位置
            #region 2.Card里面增加按钮
            IUFCard card = (IUFCard)part.GetUFControlByName(part.TopLevelContainer, "Card0");
            btnPRToPM2 = new UFWebButtonAdapter();
            btnPRToPM2.Text = "拉单销售订单";
            btnPRToPM2.ID = "BtnQurySaleOrder1";
            btnPRToPM2.AutoPostBack = true;
            card.Controls.Add(btnPRToPM2);
            //btnPRToPM2.Click += new EventHandler(BtnPRToPM2_Click);
            CommonFunction.Layout(card, btnPRToPM2, 16, 0);   //一般为从左往右按钮个数乘以2
            #endregion

            //3、Card的DropDownButton里面添加按钮
            #region Card的DropDownButton里面添加按钮
            IUFMenu btnPRToPM1 = new UFWebMenuAdapter();
            btnPRToPM1.Text = "拉单销售订单";
            btnPRToPM1.ID = "BtnQurySaleOrder2";
            btnPRToPM1.AutoPostBack = true;
            IUFDropDownButton menuButtion = (IUFDropDownButton)CommonFunction.FindControl(part, "Card0", "DDBtnOperation");
            if (menuButtion != null)
            {
                btnPRToPM1.ItemClick += BtnPRToPM1_ItemClick;
                menuButtion.MenuItems.Add(btnPRToPM1);
            }
            #endregion

            //增加刷新按钮
            AddBtnReload();
        }

        //添加刷新按钮
        private void AddBtnReload()
        {
            BtnCusReload.Text = "刷新页面";
            BtnCusReload.ID = "BtnCusReload";
            BtnCusReload.AutoPostBack = true;
            BtnCusReload.Visible = false; //不显示
            IUFCard uFControlByName = (IUFCard)this.part.GetUFControlByName(this.part.TopLevelContainer, "Card0");
            uFControlByName.Controls.Add(BtnCusReload);
            CommonFunction.Layout(uFControlByName, BtnCusReload, 20, 0);
            BtnCusReload.Click += (new EventHandler(this.BtnCusReload_Click));

        }

        //当前界面刷新方法
        private void BtnCusReload_Click(object sender, EventArgs e)
        {
            IUIRecord rec = this.part.Model.Views[0].FocusedRecord;
            this.part.Model.ClearErrorMessage();

            if (rec != null && rec["ID"] != null && long.Parse(rec["ID"].ToString()) > 0)
                (part.Action as BaseAction).NavigateAction.MovePageAt(null, long.Parse(rec["ID"].ToString()));
        }

        private void BtnPRToPM1_ItemClick(object sender, MenuItemClickEventArgs e)
        {
            try
            {
                //弹出画面,通过CurrentState传入页面级缓存,关闭后刷新主画面
                IUIRecord rec = this.part.Model.Views[0].FocusedRecord;
                this.part.CurrentState["DocumentTypeCode"] = rec["DocumentType_Code"].ToString();
                this.part.CurrentState["CustomerCode"] = rec["OrderBy_Code"].ToString();
                this.part.CurrentState["SODocNo"] = rec["DocNo"] != null ? rec["DocNo"].ToString() : string.Empty;
                this.part.ShowAtlasModalDialog(this.BtnCusReload, "8cbd57df-2209-4cf3-a94d-5b8b66be1375", "销售合同", " 992", "504", null, null, true, false, false);
            }
            catch(Exception ex)
            {
                //前端报错,方法支持具体字段
                SetErrorMsg(this.part.Model.Receivement.FocusedRecord, null, ex.Message);
            }
        }

        private void BtnPRToPM2_Click(object sender, EventArgs e)
        {
            throw new NotImplementedException();
        }

        //按钮事件,各代码示例
        void btnPRToPM_Click(object sender, EventArgs e)
        {
            try
            {
                //数据收集/绑定
                this.part.DataCollect();
                this.part.IsDataBinding = true;
                this.part.IsConsuming = false;

                //调用默认保存
                (part.Action as BaseAction).CommonAction.Save();
                //清除前端报错
                this.part.Model.ClearErrorMessage();
                //刷新界面
                (part.Action as BaseAction).NavigateAction.Refresh(null);
                
            }
            catch (Exception ex)
            {
                //前端报错,方法支持具体字段
                SetErrorMsg(this.part.Model.Receivement.FocusedRecord, null, ex.Message);
            }
            
        }

        public void SetErrorMsg(IUIRecord record, string attri, string eMsg)
        {
            if (record != null && !string.IsNullOrEmpty(attri))
            {
                this.part.Model.ErrorMessage.SetErrorMessage(ref record, attri, eMsg);
            }
            IUIModel mdl = this.part.Model as IUIModel;
            this.part.Model.ErrorMessage.SetErrorMessage(ref mdl, eMsg);
        }

        public override void BeforeEventProcess(IPart Part, string eventName, object sender, EventArgs args, out bool executeDefault)
        {
            
            #region 在按钮点击前事件,添加判断逻辑
            try
            {
                UFSoft.UBF.UI.WebControlAdapter.UFWebButton4ToolbarAdapter webButton = sender as UFSoft.UBF.UI.WebControlAdapter.UFWebButton4ToolbarAdapter;
                if (webButton != null && webButton.Action == "SubmitClick")
                {
                    if (part.Model.Receivement.FocusedRecord.ID < 0)
                        throw new Exception("当前单位为新增状态");
                }
            }
            catch (Exception ex)
            {
                foreach (IUIRecord rec in this.part.Model.Views[0].Records)
                {
                    SetErrorMsg(rec, null, ex.Message);
                }
            }
            #endregion
            base.BeforeEventProcess(Part, eventName, sender, args, out executeDefault);
        }

        public override void AfterEventProcess(UFSoft.UBF.UI.IView.IPart Part, string eventName, object sender, EventArgs args)
        {

            
            #region 获取提交按钮点击后事件，加自己的后续处理
            try
            {
                UFSoft.UBF.UI.WebControlAdapter.UFWebButton4ToolbarAdapter webButton = sender as UFSoft.UBF.UI.WebControlAdapter.UFWebButton4ToolbarAdapter;
                if (webButton != null && webButton.Action == "SubmitClick")
                {
                    //传递缓存数据
                    part.CurrentSessionState["XXX"] = "xxx";//Session级别
                    part.CurrentState["XXX"] = "xxx";//Page级别

                    //调用后台BP
                    //if (rec["ItemInfo_ItemCode"] != null && !string.IsNullOrEmpty(rec["ItemInfo_ItemCode"].ToString()))
                    //{
                    //    OperGetItemSpecsByItemCodeProxy operGetItemSpecsByItemCodeProxy = new OperGetItemSpecsByItemCodeProxy();
                    //    operGetItemSpecsByItemCodeProxy.ItemCode = rec["ItemInfo_ItemCode"].ToString();
                    //    this._part.CurrentState["ItemInfo_ItemID_SPECS"] = operGetItemSpecsByItemCodeProxy.Do();
                    //}

                    //下端提示框消息
                    if (this.part.Model.Receivement.FocusedRecord.ID < 0)
                    {
                        this.part.ShowWindowStatus("当前单据为新增状态");
                    }

                }
            }catch (Exception ex)
            {
                foreach (IUIRecord rec in this.part.Model.Views[0].Records)
                {
                    SetErrorMsg(rec, null, ex.Message);
                }
            }
            
            
            #endregion
            base.AfterEventProcess(Part, eventName, sender, args);

        }

        public override void AfterRender(IPart Part, EventArgs args)
        {
            base.AfterRender(Part, args);
        }

        public override void BeforeRender(IPart Part, EventArgs args)
        {
            base.BeforeRender(Part, args);
        }

        public override void BeforeDataBinding(IPart Part, out bool executeDefault)
        {
            base.BeforeDataBinding(Part, out executeDefault);
        }

        public override void AfterDataBinding(IPart Part)
        {
            base.AfterDataBinding(Part);
            #region 设置自定义按钮状态
            if (this.part.Model.Receivement.FocusedRecord != null && this.part.Model.Receivement.FocusedRecord.Status == 1)
            {
                btnPRToPM2.Enabled = false;
            }
            #endregion
        }


    }
}
